<?php
namespace model;
use model\DBConnection;
use PDOException;
use \PDO;
class Query {
	private $connection;
	
	public function __construct() {
		$this->connection = DBConnection::getDBConnection();
	}
	public function updateTeritory($game_id, $teritory_id, $player_id) {
		try {
			$statement = "insert into teritory_distribution (game_id, teritory_id, player_id)
							values(:GAME, :TERITORY, :PLAYER)";
			$stmt = $this->connection->prepare($statement);
			$stmt->bindValue(":GAME", $game_id, PDO::PARAM_STR);
			$stmt->bindValue(":TERITORY", $teritory_id, PDO::PARAM_STR);
			$stmt->bindValue(":PLAYER", $player_id, PDO::PARAM_STR);
			$stmt->execute();
		
		} catch (PDOException $e) {
			 $e->getMessage();
		}
	}
	
	public function insertUser($id) {
		try {
			$statement = "select ";
			$stmt = $this->connection->prepare($statement);
			$stmt->bindValue(":GAME", $game_id, PDO::PARAM_STR);
			$stmt->bindValue(":TERITORY", $teritory_id, PDO::PARAM_STR);
			$stmt->bindValue(":PLAYER", $player_id, PDO::PARAM_STR);
			$stmt->execute();
		
		} catch (PDOException $e) {
			echo $e->getMessage();
		}
	}
	
	public function getQuestionType1 ($game_id, $type) {
		try {
		    $statement = "select *
							from question
						   where type = :TYPE and question_id not in (select question_id 
																				from question_asked
													 							where game_id = :GAME)
						   order by rand()
						   limit 1";
			$stmt = $this->connection->prepare($statement);
			$stmt->bindValue(":GAME", $game_id, PDO::PARAM_INT);
			$stmt->bindValue(":TYPE", $type, PDO::PARAM_INT); 
			$stmt->execute();	
			$result = 	$stmt->fetch();
			return $result;
		} catch (PDOException $e) {
			echo $e->getMessage();
		}
	} 
	
	public function getAnswers ($question_id) {
		try {
			$statement = "select *
							from answer
						   where question_id = :QUESTION_ID";
			$stmt = $this->connection->prepare($statement);
			$stmt->bindValue(":QUESTION_ID", $question_id, PDO::PARAM_INT);
			$stmt->execute();
			$result = 	$stmt->fetchAll();
			return $result;
		} catch (PDOException $e) {
			echo $e->getMessage();
		}
	}
	
	public function getCorrectAnswer($question_id) {
		try {
			$statement = "select answer_id, answer_text
							from answer
						   where question_id = :QUESTION_ID and correct_answer = 1";
			$stmt = $this->connection->prepare($statement);
			$stmt->bindValue(":QUESTION_ID", $question_id, PDO::PARAM_INT);
			$stmt->execute();
			$result = $stmt->fetch();
			return $result;
		} catch (PDOException $e) {
			echo $e->getMessage();
		}
	}
	
	public function insertQuestionAsked ($game_id, $question_id) {
		try {
			$statement = "insert into question_asked (game_id, question_id)
										values (:GAME_ID, :QUESTION_ID)";
			$stmt = $this->connection->prepare($statement);
			$stmt->bindValue(":QUESTION_ID", $question_id, PDO::PARAM_INT);
			$stmt->bindValue(":GAME_ID", $game_id, PDO::PARAM_INT);
			$stmt->execute();
		} catch (PDOException $e) {
			echo $e->getMessage();
		}
	}
}